package com.example.maggie.meizinote.mvp.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.GravityCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.maggie.meizinote.app.MApp;
import com.example.maggie.meizinote.app.bean.note_item;
import com.example.maggie.meizinote.mvp.ui.adapter.NoteListAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.example.maggie.meizinote.di.component.DaggerSearchComponent;
import com.example.maggie.meizinote.di.module.SearchModule;
import com.example.maggie.meizinote.mvp.contract.SearchContract;
import com.example.maggie.meizinote.mvp.presenter.SearchPresenter;

import com.example.maggie.meizinote.R;


import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class SearchActivity extends BaseActivity<SearchPresenter> implements SearchContract.View {
    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;
    @BindView(R.id.edit)
    EditText editText;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.layout)
    LinearLayout layout;
    @Inject
    NoteListAdapter adapter;

    @OnClick(R.id.search)
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.search:
                mPresenter.search(editText.getText().toString());
                break;
        }
    }

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerSearchComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .searchModule(new SearchModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_search; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    public void dayOrNight() {
        if (MApp.isNight) {
            toolbar.setBackgroundColor(getResources().getColor(R.color.night_toolbar_color));
            layout.setBackgroundColor(getResources().getColor(R.color.night_bg_color));
        } else {
            toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            layout.setBackgroundColor(getResources().getColor(R.color.day_bg_color));
        }
        setColor();
    }

    public void setColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            if (MApp.isNight)
                window.setStatusBarColor(getResources().getColor(R.color.night_toolbar_color));
            else
                window.setStatusBarColor(getResources().getColor(R.color.colorPrimary));
        }
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        dayOrNight();
        setAdapter(recycler_view, adapter);
    }

    private void setAdapter(RecyclerView recycler_view, BaseQuickAdapter adapter) {
        recycler_view.setLayoutManager(new LinearLayoutManager(getApplicationContext()));// 布局管理器
        recycler_view.setAdapter(adapter);
        recycler_view.setItemAnimator(new DefaultItemAnimator());
        adapter.setOnItemClickListener(mPresenter);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void addData(List<note_item> object) {
        adapter.setNewData(object);
        adapter.bindToRecyclerView(recycler_view);
        adapter.setEmptyView(R.layout.view_empty);
    }
}
